#pragma once

/*#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>

#include "Log.hpp"
#include "Common.hpp"
#include "InetAddr.hpp"

using namespace LogMudule;

const std::string gpath = "./";
const std::string gdictname = "dict.txt";
const std::string gsep = ": ";

class Dictionary
{

public:
    Dictionary(const std::string &path = gpath, const std::string &filename = gdictname)
        : _path(path),
          _filename(filename)

    {
        LoadDictionary();
    }

    bool LoadDictionary()
    {
        std::string file = _path + _filename;
        std::ifstream in(file.c_str());
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << "open file" << file << "error";
            return false;
        }
        std::string line;
        while (std::getline(in, line)) // getline会把一整行都交给line对象
        {
            std::string key;
            std::string value;
            if (SplitString(line, &key, &value, gsep))
            {
                _dictionary.insert(std::make_pair(key, value));
            }
        }

        in.close();
        return true;
    }

    std::string Translate(const std::string &word)
    {
        auto iter = _dictionary.find(word);
        if (iter == _dictionary.end())
            return "NULL";
        std::cout << "|" << word << "|" << std::endl;
        return iter->second;
    }

    void Print()
    {
        for (auto &item : _dictionary)
        {
            std::cout << item.first << ":" << " " << item.second << std::endl;
        }
    }

    Dictionary *get()
    {
        return this;
    }

    ~Dictionary()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dictionary;
    std::string _path;
    std::string _filename;
};*/

// 创建一个模块，翻译模块，接收到单词后返回中文意思

#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include "Log.hpp"

// 默认读取文件的路径和文件名称
using namespace LogMudule;

const std::string gpath = "./";
const std::string gfilename = "dict.txt";
const std::string gsep = ": ";
#define GPATH gpath
#define GFILENAME gfilename

class Dictionary
{
public:
    Dictionary(const std::string path = GPATH, const std::string filename = GFILENAME)
        : _path(GPATH),
          _filename(GFILENAME)
    {
        LoadDictionary();
    }
    // 加载词典
    bool LoadDictionary()
    {
        const std::string DictPath = _path + _filename;
        std::ifstream in(DictPath);
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << DictPath << "open filed";
        }
        else
        {
            std::string line;
            while (std::getline(in, line))
            {
                std::string key;
                std::string value;

                if (SplitString(line, &key, &value, gsep))
                {
                    _dict.insert(std::make_pair(key, value));
                }
            }
            in.close();
            return true;
        }
    }

    std::string Translate(const std::string word)
    {
        for (const auto &dict : _dict)
        {
            if (dict.first == word)
            {
                std::cout << "|" << word << "|" << std::endl;
                return dict.second;
            }
        }
        return "NULL";
    }

    /* std::string Translate(const std::string &word)
     {
         auto iter = _dict.find(word);
         if (iter == _dict.end())
             return "NULL";
         std::cout << "|" << word << "|" << std::endl;
         return iter->second;
     }*/

    void Print()
    {
        for (auto &item : _dict)
        {
            std::cout << item.first << ":" << " " << item.second << std::endl;
        }
    }

    Dictionary *get()
    {
        return this;
    }

    ~Dictionary()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _path;
    std::string _filename;
};